package com.ebai.product.management.repository.system;

import java.util.List;

import javax.persistence.Table;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.ebai.product.management.model.entity.Auth;

@Repository
@Table(name="auth")
@Qualifier("authRepository")
public interface AuthRepository extends CrudRepository<Auth, String>, JpaSpecificationExecutor<Auth> {

    public Auth save(Auth auth);

    public void deleteByPid(String pid);

    public void delete(Auth entity);

    public Auth findByPid(String pid);

    public List<Auth> findAll();
    
    @Query("select t from Auth t where t.isDelete='0' order by t.sortOrder")
    public List<Auth> findAuthsAll();

    @Query("select t from Auth t where t.isDelete='0' and t.authType!='BUTTON' order by t.sortOrder")
    public List<Auth> findAuthsAllMenu();

    Page<Auth> findAll(Specification<Auth> specification, Pageable pageable);

    @Query("select t from Auth t where t.displayName=:displayName")
    public Auth findRoleByDisplayName(@Param("displayName") String displayName);

    @Query("select t from Auth t where t.displayName=:displayName")
    public List<Auth> queryRoleListByDisplayName(@Param("displayName") String displayName);
    
    @Query(nativeQuery = true,value = "select distinct m.pid, m.parent_auth, m.display_name, m.direct_url, m.auth_perms , m.auth_type "
    		+ "from t_global_auth m "
    		+ "left join t_global_role_auth rm on m.pid = rm.auth_pid "
    		+ "left join t_global_role_user ur on rm.role_pid = ur.role_pid "
    		+ "where ur.user_pid = :userPid and m.auth_type in ('menu', 'url') and m.is_delete = 0 order by m.sort_order")
    public List<Object> selectAuthsByUserId(@Param("userPid") String userPid);
    
    @Query(nativeQuery = true,value = "select distinct m.auth_perms "
    		+ "from t_global_auth m "
    		+ "left join t_global_role_auth rm on m.pid = rm.auth_pid "
    		+ "left join t_global_role_user ur on rm.role_pid = ur.role_pid "
    		+ "where ur.user_pid = :userPid and m.auth_perms!='' ")
    public List<Object> selectPermsByUserId(@Param("userPid") String userPid);
    
    @Query(nativeQuery = true,value = "select a.pid,a.parent_auth,a.display_name,a.auth_type,a.auth_perms,a.direct_url,a.sort_order,b.role_pid "
    		+ "from t_global_auth a left join t_global_role_auth b on a.pid = b.auth_pid and b.role_pid = :rolePid "
    		+ "where find_in_set(a.pid,getchildrenauth(0)) and a.is_delete='0' and a.pid!='0' order by a.sort_order ")
    public List<Object> selectAuthsTreeTable(@Param("rolePid") String rolePid);
}
